package com.x.reader.dao;

import com.x.framework.Base;
import com.x.framework.dao.BaseDao;
import com.x.reader.model.Book;
import com.x.reader.model.Favourite;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class BookDao extends BaseDao {

    private final static String selectBookRankListSql = "select t2.book_id, t2.book_title, t2.book_cover, t2.book_file, t1.book_create_time from reader_book_rank t1, reader_book t2 where t1.book_id = t2.book_id order by t1.book_create_time desc ";

    public List<Book> selectBookRankList(Book book) throws Exception {
        StringBuilder sb = new StringBuilder(selectBookRankListSql);
        List params = new ArrayList();
        return this.queryForList(sb.toString(), Book.class, params, book);
    }

    private final static String selectBookRecommendListSql = "select t2.book_id, t2.book_title, t2.book_cover, t2.book_file, t1.book_create_time from reader_book_recommend t1, reader_book t2 where t1.book_id = t2.book_id order by t1.book_create_time desc ";

    public List<Book> selectBookRecommendList(Book book) throws Exception {
        StringBuilder sb = new StringBuilder(selectBookRecommendListSql);
        List params = new ArrayList();
        return this.queryForList(sb.toString(), Book.class, params, book);
    }

    private final static String selectBookNewListSql = "select t2.book_id, t2.book_title, t2.book_cover, t2.book_file, t1.book_create_time from reader_book_new t1, reader_book t2 where t1.book_id = t2.book_id order by t1.book_create_time desc ";

    public List<Book> selectBookNewList(Book book) throws Exception {
        StringBuilder sb = new StringBuilder(selectBookNewListSql);
        List params = new ArrayList();
        return this.queryForList(sb.toString(), Book.class, params, book);
    }

    private final static String selectBookListSql = "select t1.* from reader_book t1, reader_category t2 where t1.category_id = t2.category_id ";

    public List<Book> selectBookList(Book book) throws Exception {
        StringBuilder sb = new StringBuilder(selectBookListSql);
        List params = new ArrayList();
        if (Base.isNotNull(book.getBookId())) {
            sb.append("and t1.BOOK_ID = ? ");
            params.add(book.getBookId());
        }
        if (Base.isNotNull(book.getCategoryId())) {
            sb.append("and t1.CATEGORY_ID = ? ");
            params.add(book.getCategoryId());
        }
        if (Base.isNotNull(book.getBookTitle())) {
            sb.append("and (t1.book_title like ? or t1.book_author like ?) ");
            params.add("%" + book.getBookTitle() + "%");
            params.add("%" + book.getBookTitle() + "%");
        }
        sb.append("order by t1.book_create_time desc");
        return this.queryForList(sb.toString(), Book.class, params, book);
    }

    private final static String selectBookNumSql = "select count(1) as num from reader_book t1, reader_category t2 where t1.category_id = t2.category_id ";

    public int selectBookNum(Book book) throws Exception {
        StringBuilder sb = new StringBuilder(selectBookNumSql);
        List params = new ArrayList();
        if (Base.isNotNull(book.getBookId())) {
            sb.append("and t1.BOOK_ID = ? ");
            params.add(book.getBookId());
        }
        if (Base.isNotNull(book.getCategoryId())) {
            sb.append("and t1.CATEGORY_ID = ? ");
            params.add(book.getCategoryId());
        }
        if (Base.isNotNull(book.getBookTitle())) {
            sb.append("and (t1.book_title like ? or t1.book_author like ?) ");
            params.add("%" + book.getBookTitle() + "%");
            params.add("%" + book.getBookTitle() + "%");
        }
        return this.queryForInt(sb.toString(), params);
    }

}
